package com.softer.chengxin.dicinfo.dao.provider;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.dicinfo.entity.DicInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zcz
 * @date 2018/12/20
 */
public class DicInfoDaoProvider {
    /**
     * 字典信息列表
     */
    public String getList(Page page){
        StringBuffer sql = new StringBuffer(10);
        DicInfo dicInfo = (DicInfo) page.getQueryBean();
        sql.append("SELECT id,`name`,`code`,`type`,`adder`,DATE_FORMAT(add_time, '%Y-%m-%d') AS `addTime`,`description` FROM dic_info ");
        sql.append(" WHERE `code` LIKE CONCAT("+dicInfo.getTypeCode()+",'%') ");
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }
    /**
     * 修改角色信息
     * @param role
     * @return
     */
    public String update(DicInfo role) {
        StringBuffer sql = new StringBuffer(20);
        sql.append("UPDATE dic_info SET add_time=NOW() ");
        commonSql(sql, role);
        return sql.toString();
    }
    private void commonSql(StringBuffer sql, DicInfo dicInfo) {
        if(StringUtils.isNotBlank(dicInfo.getAdder())){
            sql.append(" ,`adder`=#{adder} ");
        }
        if(StringUtils.isNotBlank(dicInfo.getName())){
            sql.append(" ,`name`=#{name} ");
        }
        if(StringUtils.isNotBlank(dicInfo.getCode())){
            sql.append(" ,`code`=#{code} ");
        }
        if(StringUtils.isNotBlank(dicInfo.getType())){
            sql.append(" ,`type`=#{type} ");
        }
        if(StringUtils.isNotBlank(dicInfo.getDescription())){
            sql.append(" ,`description`=#{description} ");
        }
        sql.append(" where id=#{id}");
    }
}
